<?php
class MylinkAction extends AdminAction{
	//取出一级栏目
	protected function types(){
		$menu=M('Menu');
		$type=$menu->where('pid=0')->field('typeid,name')->select();
		for($i=0,$j=count($type);$i<$j;$i++){
			$types[$type[$i]['typeid']]=$type[$i]['name'];
		}
		//在数组第一个值加上999
		$a=array('全局'=>999);
		$types=array_flip($types);
		$types=array_merge($a,$types);
		$types=array_flip($types);

		return $types;
	}

	protected function selected(){
		$type=$this->types();
		$this->assign('type',$type);
	}

	protected function db(){
		$mylink=M('Mylink');
		return $mylink;
	}

	//内链列表
	public function index(){
		//导入栏目下拉菜单
		$this->selected();
		//查询内链列表
		$mylink=$this->db();
		$list=$mylink->select();
		//整合栏目名
		$j=count($list);
		for($i=0;$i<$j;$i++){
			$list[$i]['typename']=$this->typename($list[$i]['typeid']);
		}
		//输出
		$this->assign('selected',$_GET['id']);
		$this->assign('list',$list);
		$this->display();
	}

	//按分类查看
	public function type(){
		//导入栏目下拉菜单
		$this->selected();
		//查询内链列表
		$mylink=$this->db();
		if(!$_GET['id']){
			$list=$mylink->select();
		}else{
			$list=$mylink->where(array('typeid'=>$_GET['id']))->select();
		}
		
		//整合栏目名
		$j=count($list);
		for($i=0;$i<$j;$i++){
			$list[$i]['typename']=$this->typename($list[$i]['typeid']);
		}
		//输出
		$this->assign('selected',$_GET['id']);
		$this->assign('list',$list);
		$this->display('index');
	}

	//内链添加
	public function add(){
		if($_GET['id']==999){
			$typename='全局';
		}elseif($_GET['id']==0 || $_GET['id']==Null){
			$this->error('请选择要添加的栏目');
		}else{
			$typename=$this->typename($_GET['id']);		
		}
		$this->assign('typename',$typename);
		$this->display();
	}
	public function adding(){
		$mylink=$this->db();
		$result=$mylink->add($_POST);
		if($result){
			$this->success('添加成功','index');
		}else{
			$this->error('添加失败');
		}
	}

	//内链编辑
	public function edit(){
		$mylink=$this->db();
		$result=$mylink->where(array('id'=>$_GET['id']))->find();
		$this->assign('list',$result);

		$typename=$this->typename($result['typeid']);
		$this->assign('typename',$typename);
		$this->display();
	}

	public function editing(){
		if($_POST['weight']<0 || $_POST['weight']>9){
			$this->error('对不起，您填写的权重值超出范围！');
		}
		$mylink=$this->db();
		$result=$mylink->where(array('id'=>$_POST['id']))->save($_POST);
		if($result){
			$this->success('保存成功','index');
		}else{
			$this->error('保存失败');
		}
	}


}